﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace ProjectM2Work
{
    public abstract class GameScreen
    {
        #region Properties
        List<GamePageComponent> components = new List<GamePageComponent>();
        protected GamePage _maingame;
        private bool _visible; //True thì vẽ

        public bool Visible
        {
            get { return _visible; }
            set { _visible = value; }
        }
        private bool _enabled; //True thì update

        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }
        protected SpriteBatch _spriteBatch;
        public List<GamePageComponent> Components
        {
            get { return components; }
        }
        #endregion

        #region  Initilation
        public GameScreen(GamePage maingame, SpriteBatch spriteBatch)
        {
            this._spriteBatch = spriteBatch;
            this._maingame = maingame;
        }

        #endregion

        #region  Delegate + event
        // Y nghia: Khi gamescreen nay duoc kich hoat ( show ) thi cac game screen khac phai hide di
        public delegate void IsShowed(int index);
        public event IsShowed Showed;
        /*
        * gamescreen.Showed += new GameScreen.IsShow(FunctionIsCalled);         
        */

        #endregion

        #region Methods

        public virtual void Show()
        {
            this.Visible = true;
            this.Enabled = true;
            foreach (GamePageComponent component in components)
            {
                component.Enabled = true;
                component.Visible = true;
            }
        }

        public virtual void Hide()
        {
            this.Visible = false;
            this.Enabled = false;
            foreach (GamePageComponent component in components)
            {
                component.Enabled = false;
                component.Visible = false;
            }
        }

        public virtual void ResetScreen(int input) { }
        #endregion

        #region Update & Draw
        public virtual void OnUpdate(GameTimerEventArgs gameTime)
        {
            //foreach (GameComponent component in components)
            if (this.Enabled)
            {
                for (int i = 0; i < components.Count; i++)
                    if (components[i].Enabled == true)
                        components[i].OnUpdate(gameTime);
            }
            
        }

        public virtual void OnDraw(GameTimerEventArgs gameTime)
        {
            if (this.Visible)
            {
                foreach (GamePageComponent component in components)
                    if (component.Visible)
                        component.OnDraw(_spriteBatch, gameTime);
            }            
        }
        #endregion       
    }
}
